package cz.zcu.fav.ratdroid.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import cz.zcu.fav.ratdroid.R;
import cz.zcu.fav.ratdroid.activity.SearchActivity.SearchType;
import cz.zcu.fav.ratdroid.bean.Object;
import cz.zcu.fav.ratdroid.service.RestService;

/**
 * Contains list of object loaded via several methods: <br>
 * - from favourites <br>
 * - via fulltext search <br>
 * - via QR code
 * 
 * @author Lukas Gemela
 * 
 */
public class ObjectBrowserActivity extends AbstractObjectsListActivitity
{
	public static int OBJECT_BROWSER_REQUEST_CODE = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		getListView().setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				// on item click put it into intend and make a stapback
				// its our caller bussiness what to do with this object
				Intent returnIntent = new Intent();
				setResult(RESULT_OK, returnIntent);
				returnIntent.putExtra(Object.SINGLE_OBJECT_IDENTIFIER,
						getLoadedObjects().get(position));
				finish();
			}
		});

		registerForContextMenu(getListView());

		if (!isRecreated())
		{
			getListView().post(new Runnable()
			{
				public void run()
				{
					openContextMenu(getListView());
				}
			});

		}
	}

	/**
	 * override standard behaviour and show context menu on menu click button
	 */
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_MENU)
		{
			openContextMenu(getListView());
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo)
	{
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.object_browser_menu, menu);
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent)
	{
		IntentResult scanResult = IntentIntegrator.parseActivityResult(
				requestCode, resultCode, intent);
		if (scanResult != null && scanResult.getContents() != null)
		{
			// handle qr scan result
			setRestTask(RestService.getInstance().getObjectsByIdentifier(this,
					scanResult.getContents(), this));
		} else
		// else continue with any other code
		if (requestCode == SearchActivity.SEARCH_REQUEST_CODE)
		{
			if (resultCode == RESULT_OK)
			{
				String searchKey = intent
						.getStringExtra(SearchActivity.SEARCH_STRING_KEY);
				SearchType searchBy = (SearchType) intent
						.getSerializableExtra(SearchActivity.SEARCH_BY_KEY);
				setRestTask(RestService.getInstance().seachObjects(this,
						searchKey, searchBy, this));
			}
		}
	}

	public boolean onContextItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.menu_favourites:
			//load favourites
			setRestTask(RestService.getInstance().getFavourites(this, this));
			break;
		case R.id.menu_fulltext:
			//start search activity
			Intent k = new Intent(this, SearchActivity.class);
			startActivityForResult(k, SearchActivity.SEARCH_REQUEST_CODE);
			break;
		case R.id.menu_qr_code:
			//start QR reader
			IntentIntegrator integrator = new IntentIntegrator(this);
			integrator.initiateScan();
			break;
		default:
			return super.onContextItemSelected(item);
		}
		return super.onContextItemSelected(item);

	}

	@Override
	protected List<Object> provideData()
	{
		// initial object collection is empty
		return new ArrayList<Object>();
	}
}
